define([
    'app',
    'css!css/core/login'
], function(app) {
    app.controller('RegisterController', ['Dialog', 'AuthService', 'KaptchaProvider',
        function(Dialog, AuthService, KaptchaProvider) {
        var self = this;

        self.user = {
            name: '',
            email: '',
            password: '',
            confirmPassword: '',
            kaptcha: ''
        };

        self.kaptcha = KaptchaProvider.load();
        self.changeKaptcha = function() {
            self.kaptcha = KaptchaProvider.refresh();
        };

        self.regist = function(valid) {
            if (valid) {
                if (self.user.password != self.user.confirmPassword) {
                    return;
                }
                AuthService.regist(self.user).then(function(resp) {
                    if (resp.success) {
                        Dialog.$alert('注册成功！').then(function() {
                            location.href = '#/login';
                        });
                    } else {
                        self.changeKaptcha();
                    }
                });
            }
        }
    }]);
});
